package com.atguigu03.stream;

import org.junit.Test;

import java.util.List;
import java.util.stream.Stream;

public class StreamAPITest2 {
    //1 匹配与查找
    @Test
    public void t1(){
        List<Employee> employeeList = EmployeeData.getEmployees();
        Stream<Employee> employeeStream = employeeList.stream();
//allMatch(Predicate p)——检查是否匹配所有元素。
        System.out.println(employeeStream.allMatch(employee -> employee.age > 18));
//练习:是否所有的员工的年龄都大于18
        Stream<Employee> employeeStream1 = employeeList.stream();
        System.out.println(employeeStream1.anyMatch(employee -> employee.age > 18));
       // employeeStream.filter(employee -> employee.age>18).forEach(System.out::println);
//anyMatch(Predicate p)—-检查是否至少匹配一个元素。
//练习:是否存在员工的工资大于10000
        Stream<Employee> employeeStream2 = employeeList.stream();
        System.out.println(employeeStream2.anyMatch(employee -> employee.getSalary() > 10000));
// findFirst—-返回第一个元素
        Stream<Employee> employeeStream3 = employeeList.stream();
        System.out.println(employeeStream3.findFirst());

    }
    @Test
    public void t2(){
        // count—-返回流中元素的总个数
        List<Employee> list = EmployeeData.getEmployees();
        System.out.println(list.stream(). count());

       // max (Comparator c)——返回流中最大值
        //返回最高工资的员工
        System.out.println(list.stream().max((e1, e2) -> Double.compare(e1.getSalary(),e2.getSalary())));
        // 练习:返回最高的工资:
        System.out.println(list.stream().map(employee -> employee.getSalary( )).max((salary1, salary2) -> Double.compare(salary1, salary2)).get());
        //min(Comparator c)—返回流中最小值
        //练习:返回最低工资的员工
        System.out.println(list.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).get());


        //forEach(Consumer c)——内部迭代

    }
    //规约
@Test
    public void t3(){
   // reduce(T identity，BinaryOperator)--可以将流中元素反复结合起来，得到一个值。返回 T
    // 练习1: 计算1-10的自然数的和
   // reduce(BinaryOperator) -可以将流中元素反复结合起来，得到一个值。返回 Optional<T>
    // 练习2: 计算公司所有员工工资的总和
}
}
